using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using xna_game_engine.source;


namespace xna_game_engine.source
{
    public class GraphicsSystem : Microsoft.Xna.Framework.GameComponent
    {
        // Members
        private GraphicsDeviceManager mGraphics;
        private SpriteBatch mSpriteBatch;
        private Dictionary<string, SpriteFont> fontLookup;
        private SpriteFont mSpriteFont;

        public GraphicsSystem(Game game, string title, int h, int w) : base(game)
        {
            mGraphics = new GraphicsDeviceManager(game);
            mGraphics.PreferredBackBufferWidth = h;
            mGraphics.PreferredBackBufferHeight = w;
            Game.Window.Title = title;
        }

        public override void Initialize()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            mSpriteBatch = new SpriteBatch(Game.GraphicsDevice);
            fontLookup = new Dictionary<string, SpriteFont>();
            mSpriteFont = Game.Content.Load<SpriteFont>("fonts\\test_font");
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            Game.GraphicsDevice.Clear(Color.Black);
            base.Update(gameTime);
        }

        public void ClearScreenToColor(Color color)
        {
            // TODO: Add your update code here
            Game.GraphicsDevice.Clear(color);
        }

        public void BeginSprites()
        {
            mSpriteBatch.Begin();
        }

        //public void BeginSprites(SpriteBlendMode blend, SpriteSortMode sort, SaveStateMode save, Matrix mat)
        //{
        //    mSpriteBatch.Begin(blend, sort, save, mat);
        //}

        public void EndSprites()
        {
            mSpriteBatch.End();
        }

        public void DrawText(string text, Vector2 pos, Color color)
        {
            mSpriteBatch.DrawString(mSpriteFont, text, pos, color);
        }

        public void DrawImage(Texture2D texture, Vector2 pos, Color color)
        {
            mSpriteBatch.Draw(texture, pos, color);
        }

        public void DrawImage(Texture2D text, Vector2 pos, Rectangle src, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float depth)
        {
            mSpriteBatch.Draw(text, pos, src, color, rotation, origin, scale, effects, depth);
        }
        
        public void DrawAnimation(CAnimation anim, Texture2D texture, Vector2 pos, Color color)
        {
            mSpriteBatch.Draw(texture, pos, anim.SourceRectangle, color);
        }

        #region Properties
        public SpriteBatch SpriteBatch
        {
            get { return mSpriteBatch; }
        }
        public SpriteFont SpriteFont
        {
            get { return mSpriteFont; }
        }
        #endregion
    }
}